package cn.iocoder.yudao.module.handguard.service.productaccessrecord;

import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.handguard.dal.dataobject.applicationrecord.ApplicationRecordDO;
import cn.iocoder.yudao.module.handguard.dal.mysql.applicationrecord.ApplicationRecordMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.handguard.controller.admin.productaccessrecord.vo.*;
import cn.iocoder.yudao.module.handguard.dal.dataobject.productaccessrecord.ProductAccessRecordDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.handguard.convert.productaccessrecord.ProductAccessRecordConvert;
import cn.iocoder.yudao.module.handguard.dal.mysql.productaccessrecord.ProductAccessRecordMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.handguard.enums.ErrorCodeConstants.*;

/**
 * 产品准入-产品备注/产品特点记录 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class ProductAccessRecordServiceImpl implements ProductAccessRecordService {

    @Resource
    private ApplicationRecordMapper applicationRecordMapper;

    @Resource
    private ProductAccessRecordMapper productAccessRecordMapper;

    @Override
    public Long createProductAccessRecord(ProductAccessRecordCreateReqVO createReqVO) {
        // 插入
        ProductAccessRecordDO productAccessRecord = ProductAccessRecordConvert.INSTANCE.convert(createReqVO);
        productAccessRecordMapper.insert(productAccessRecord);
        // 返回
        return productAccessRecord.getId();
    }

    @Override
    public void updateProductAccessRecord(ProductAccessRecordUpdateReqVO updateReqVO) {
        // 校验存在
        validateProductAccessRecordExists(updateReqVO.getId());
        // 更新
        ProductAccessRecordDO updateObj = ProductAccessRecordConvert.INSTANCE.convert(updateReqVO);
        productAccessRecordMapper.updateById(updateObj);
    }

    @Override
    public void deleteProductAccessRecord(Long id) {
        // 校验存在
        validateProductAccessRecordExists(id);
        // 删除
        productAccessRecordMapper.deleteById(id);
    }

    private void validateProductAccessRecordExists(Long id) {
        if (productAccessRecordMapper.selectById(id) == null) {
            throw exception(PRODUCT_ACCESS_RECORD_NOT_EXISTS);
        }
    }

    @Override
    public ProductAccessRecordDO getProductAccessRecord(Long id) {
        return productAccessRecordMapper.selectById(id);
    }

    @Override
    public List<ProductAccessRecordDO> getProductAccessRecordList(Collection<Long> ids) {
        return productAccessRecordMapper.selectBatchIds(ids);
    }

    @Override
    public List<ProductAccessRecordDO> getProductAccessRecordByUserIdAndOrderId(Long userId, Long orderId) {
        LambdaQueryWrapperX<ProductAccessRecordDO> wrapper = new LambdaQueryWrapperX<>();
        wrapper.eq(ProductAccessRecordDO::getUserId,userId);
        wrapper.eq(ProductAccessRecordDO::getOrderNo,orderId);
        return productAccessRecordMapper.selectList(wrapper);
    }

    @Override
    public PageResult<ProductAccessRecordDO> getProductAccessRecordPage(ProductAccessRecordPageReqVO pageReqVO) {
        return productAccessRecordMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ProductAccessRecordDO> getProductAccessRecordList(ProductAccessRecordExportReqVO exportReqVO) {
        return productAccessRecordMapper.selectList(exportReqVO);
    }

}
